<?php

class CodetailsController extends AppController {

    var $name = 'Codetails';
    var $helpers = array('Cache', 'Html', 'Form', 'Text', 'Csv');
    var $paginate = array('limit' => 50);

    function before_filter() {
        parent::beforeFilter();
        //$this->FileUpload->fileModel(null);
    }

    function admin_update_date() {
        $filename = "/app/webroot/files/delisting2.csv";
        //$filename="C:/xampp/htdocs/company/app/webroot/files/delisting2.csv";
        $messages = $this->Codetail->import($filename);
        //debug($messages);
        if (isset($messages['errors']) && count($messages['errors']) > 0) {
            $msg = implode("<br>", $messages['errors']);
        } else {
            $msg = implode("<br>", $messages['messages']);
        }
        //echo $msg;
        $this->Session->setFlash($msg);
    }

    function admin_oa_import() {
        $filename = WWW_ROOT . "files/AusCosOAs.csv";
        //$filename="C:/xampp/htdocs/company/app/webroot/files/delisting2.csv";
        $messages = $this->Codetail->import($filename);
        //debug($messages);
        if (isset($messages['errors']) && count($messages['errors']) > 0) {
            $msg = implode("<br>", $messages['errors']);
        } else {
            $msg = implode("<br>", $messages['messages']);
        }
        //echo $msg;
        $this->Session->setFlash($msg);
    }

    function index() {
        $this->redirect('/admin/codetails/index');
    }

    function admin_index() {
        $this->Codetail->recursive = 0;
        $this->set('codetails', $this->paginate());
    }

    function admin_search_comid($Code=null) {
        if (!empty($this->data) || !empty($Code)) {
            $this->Codetail->recursive = -1;
            $keyword = !empty($Code) ? $Code : trim($this->data['Codetail']['keywords']);
            $this->paginate = array("conditions" => array("Codetail.ComID" => $keyword), "order" => array("Codetail.ComName" => "ASC"));
            $result = $this->paginate('Codetail');

            if (count($result) < 1) {
                $this->Session->setFlash(__('No data found with the ComID <i>"' . $keyword . '"</i>. Please, try again.', true));
            } elseif (count($result) == 1) {
                $this->redirect(array('action' => 'view', $result[0]['Codetail']['ComID']));
                exit;
            }
            $this->set('keyword', $keyword);
            $this->set('codetails', $result);
        }
    }

    function admin_search_code($Code=null) {
        if (!empty($this->data) || !empty($Code)) {
            $this->Codetail->recursive = -1;
            $keyword = !empty($Code) ? $Code : trim($this->data['Codetail']['keywords']);
            $this->paginate = array("conditions" => array("Codetail.Code" => $keyword), "order" => array("Codetail.ComName" => "ASC"));
            $result = $this->paginate('Codetail');

            if (count($result) < 1) {
                $this->Session->setFlash(__('No data found with the Code <i>"' . $keyword . '"</i>. Please, try again.', true));
            } elseif (count($result) == 1) {
                $this->redirect(array('action' => 'view', $result[0]['Codetail']['ComID']));
                exit;
            }
            $this->set('keyword', $keyword);
            $this->set('codetails', $result);
        }
    }

    function admin_search_codey($Code=null) {
        if (!empty($this->data) || !empty($Code)) {
            $this->Codetail->recursive = -1;
            $keyword = !empty($Code) ? $Code : trim($this->data['Codetail']['search_codey']);
            $this->paginate = array("conditions" => array("Codetail.CodeY" => $keyword), "order" => array("Codetail.StartDate" => "DESC"));
            $result = $this->paginate('Codetail');

            if (count($result) < 1) {
                $this->Session->setFlash(__('No data found with the CodeY <i>"' . $keyword . '"</i>. Please, try again.', true));
            } elseif (count($result) == 1) {
                $this->redirect(array('action' => 'view', $result[0]['Codetail']['ComID']));
                exit;
            }
            $this->set('keyword', $keyword);
            $this->set('codetails', $result);
        }
        //$this->render("admin_search_code");
    }

    function admin_search_comname($Code=null) {
        //pr($this->data);exit;
        if (!empty($this->data) || !empty($Code)) {
            $this->Codetail->recursive = -1;
            $keyword = !empty($Code) ? $Code : trim($this->data['Codetail']['search_keyword']);
            $this->paginate = array("conditions" => array("Codetail.ComName like" => "%" . $keyword . "%"), "order" => array("Codetail.ComName" => "ASC"));
            $result = $this->paginate('Codetail');

            if (count($result) < 1) {
                $this->Session->setFlash(__('No data found with the ComName <i>"' . $keyword . '"</i>. Please, try again.', true));
            } elseif (count($result) == 1) {
                $this->redirect(array('action' => 'view', $result[0]['Codetail']['ComID']));
                exit;
            }
            $this->set('keyword', $keyword);
            $this->set('codetails', $result);
        }
        //$this->render("admin_search_code");
    }

    function admin_view($id = null) {
        $this->layout = "viewlayout";
        if (!$id) {
            $this->Session->setFlash(__('Invalid codetail', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Codetail->Coevent->unbindModel(array('belongsTo' => array('Codetail')));
        $this->Codetail->Codexec->unbindModel(array('belongsTo' => array('Codetail')));
        $this->Codetail->Codtran->unbindModel(array('belongsTo' => array('Codetail')));
        $this->Codetail->recursive = 2;
        $this->data = $this->Codetail->read(null, $id);
        $this->loadModel('DirTitle');
        $titles = $this->DirTitle->findDirTitle();
        $this->loadModel('Director');
        $this->Director->recursive = -1;
        $directors = $this->Director->fetchDirector();
        //print_r($this->data);
        $this->set(compact('titles', 'directors'));
    }

    function admin_name_change($id = null) {
        $this->layout = "viewlayout";
        if (!$id) {
            $this->Session->setFlash(__('Invalid codetail', true));
            $this->redirect(array('action' => 'index'));
        }

        if (!empty($this->data)) {
            //pr($this->data);
            if ($this->data['Codetail']['StartDate']['day'] == "")
                $this->data['Codetail']['StartDate'] = '0';
            if ($this->data['Codetail']['EndDate']['day'] == "")
                $this->data['Codetail']['EndDate'] = '0';
            if ($this->data['Codetail']['delistDate']['day'] == "")
                $this->data['Codetail']['delistDate'] = '0';
            if ($this->data['Codetail']['Listed']['day'] == "")
                $this->data['Codetail']['Listed'] = '0';
            $this->Codetail->create();
            if ($this->Codetail->save($this->data)) {
                $this->Session->setFlash(__('The codetail has been saved', true));
                $new_com_id = $this->Codetail->getLastInsertId();
                $former_id = $this->data['Codetail']['former_id'];
                $this->Codetail->Coevent->unbindModel(array('belongsTo' => array('Codetail')));
                $this->Codetail->Codexec->unbindModel(array('belongsTo' => array('Codetail')));
                $this->Codetail->Codtran->unbindModel(array('belongsTo' => array('Codetail')));
                $this->Codetail->recursive = 2;
                $former_data = $this->Codetail->read(null, $former_id);
                //pr($former_data);
                /* if(count($former_data['Coevent'])>0){
                  $this->loadModel('Coevent');
                  $this->Coevent->nameChanged($former_data['Coevent'],$new_com_id);
                  } */
                if (count($former_data['Codexec']) > 0) {
                    $this->loadModel('Codexec');
                    $this->Codexec->nameChanged($former_data['Codexec'], $new_com_id);
                }
                if (count($former_data['Codtran']) > 0) {
                    $this->loadModel('Codtran');
                    $this->Codtran->nameChanged($former_data['Codtran'], $new_com_id, $this->data['Codetail']['ComName'], $this->data['Codetail']['Code']);
                }
                $this->redirect(array('action' => 'admin_view', $new_com_id));
            } else {
                $this->Session->setFlash(__('The codetail could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Codetail->read(null, $id);
        }
    }

    function admin_add() {
        if (!empty($this->data)) {
            if ($this->data['Codetail']['StartDate']['day'] == "")
                $this->data['Codetail']['StartDate'] = '0';
            if ($this->data['Codetail']['EndDate']['day'] == "")
                $this->data['Codetail']['EndDate'] = '0';
            $this->Codetail->create();
            if ($this->Codetail->save($this->data)) {
                $this->Session->setFlash(__('The codetail has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The codetail could not be saved. Please, try again.', true));
            }
        }
    }

    function admin_edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid codetail', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->data['Codetail']['StartDate']['day'] == "")
                $this->data['Codetail']['StartDate'] = '0';
            if ($this->data['Codetail']['EndDate']['day'] == "")
                $this->data['Codetail']['EndDate'] = '0';
            if ($this->Codetail->save($this->data)) {
                $this->Session->setFlash(__('The codetail has been saved', true));
                //$this->redirect(array('action' => 'index'));
                $this->redirect($_SERVER['HTTP_REFERER']);
            } else {
                $this->Session->setFlash(__('The codetail could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Codetail->read(null, $id);
        }
    }

    function admin_delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for codetail', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Codetail->delete($id)) {
            $this->Session->setFlash(__('Codetail deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Codetail was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

    function admin_export($keyword=null) {
        $this->Codetail->recursive = -1;
        // Find fields needed without recursing through associated models
        if (empty($keyword)) {
            $codetails = $this->Codetail->find(
                    'all', array('order' => "Codetail.ComID ASC"));
        } else {
            $codetails = $this->Codetail->find(
                    'all', array("conditions" => array("Codetail.ComName like" => "%" . $keyword . "%")), array('order' => "Codetail.ComName ASC"));
        } //pr($codetails);
        $this->layout = false;
        $this->autoLayout = false;
        $this->set('codetails', $codetails);
    }

    function admin_export_code($keyword=null) {
        $this->Codetail->recursive = -1;
        // Find fields needed without recursing through associated models
        if (empty($keyword)) {
            $codetails = $this->Codetail->find(
                    'all', array('order' => "Codetail.ComID ASC"));
        } else {
            $codetails = $this->Codetail->find(
                    'all', array("conditions" => array("Codetail.Code" => $keyword)), array('order' => "Codetail.ComName ASC"));
        } //pr($codetails);
        $this->layout = false;
        $this->autoLayout = false;

        $this->set('codetails', $codetails);
        $this->render("admin_export");
    }

    function admin_export_codey($keyword=null) {
        $this->Codetail->recursive = -1;
        // Find fields needed without recursing through associated models
        if (empty($keyword)) {
            $codetails = $this->Codetail->find(
                    'all', array('order' => "Codetail.ComID ASC"));
        } else {
            $codetails = $this->Codetail->find(
                    'all', array("conditions" => array("Codetail.CodeY" => $keyword)), array('order' => "Codetail.ComName ASC"));
        } //pr($codetails);
        $this->layout = false;
        $this->autoLayout = false;

        $this->set('codetails', $codetails);
        $this->render("admin_export");
    }

    function admin_upload_csv() {
        if (!empty($this->data)) {
            //pr($this->data);
            if (!empty($this->data['Codetail']['fileName']['name']) && is_uploaded_file($this->data['Codetail']['fileName']['tmp_name'])) {
                if ($this->data['Codetail']['fileName']['type'] == "text/comma-separated-values" OR $this->data['Codetail']['fileName']['type'] == "text/x-csv" OR $this->data['Codetail']['fileName']['type'] == "application/octet-stream" OR $this->data['Codetail']['fileName']['type'] == "application/vnd.ms-excel") {
                    //$fileData = fread(fopen(, "r"),$this->data['Codetail']['fileName']['size']);
                    $messages = $this->Codetail->import($this->data['Codetail']['fileName']['tmp_name']);
                    //debug($messages);
                    if (isset($messages['errors']) && count($messages['errors']) > 0) {
                        $msg = implode("<br>", $messages['errors']);
                    } else {
                        $msg = implode("<br>", $messages['messages']);
                    }
                    //echo $msg;
                    $this->Session->setFlash($msg);
                } else {
                    $this->Session->setFlash(__('Invalid file type. You must upload a CSV file.', true));
                }
            } else {
                $this->Session->setFlash(__('CSV File Required.', true));
            }
        }
    }

    function admin_download_list($id=null) {
        if (!empty($id)) {
            switch ($id) {
                case'suspended': $this->suspended($id);
                    break;
                case'in_administration': $this->in_administration($id);
                    break;
                case'in_liquidation': $this->in_liquidation($id);
                    break;
                case'deregistered': $this->deregistered($id);
                    break;
                case'delisted_nz': $this->delisted_nz($id);
                    break;
                case'listed_au': $this->listed_au($id);
                    break;
                case'listed_nz': $this->listed_nz($id);
                    break;
            }
        }
    }

    function suspended($id=null) {
        //echo $id;exit;
        $this->Codetail->recursive = -1;
        $codetails = $this->Codetail->find(
                'all', array("conditions" => array("Codetail.Exch <>" => "z", "Codetail.Cat" => "s"),
            "order" => array("Codetail.ComName ASC"))
        );

        $i = 0;
        foreach ($codetails as $codetail) {
            switch ($codetail['Codetail']['Cat']) {
                case 'l': $status = 'Listed';
                    break;
                case 's': $status = 'Suspended';
                    break;
                case 'S': $status = 'Suspended';
                    break;
                case 'd': $status = 'Delisted';
                    break;
                default: $status = "Delisted";
            }
            if (empty($codetail['Codetail']['Cat']) && isset($codetail['Codetail']['CodeY']))
                $status = "Former Name";
            if (empty($codetail['Codetail']['Cat']) && empty($codetail['Codetail']['CodeY']))
                $status = "";

            switch ($codetail['Codetail']['Exch']) {
                case 'A': $Exch = 'ASX';
                    break;
                case 'N': $Exch = 'NSX';
                    break;
                case 'B': $Exch = 'BSX';
                    break;
                case 'Z': $Exch = 'NZX';
                    break;
                case 'OZ': $Exch = 'Other';
                    break;
                case 'OA': $Exch = 'Other';
                    break;
                default: $Exch = "ASX";
            }


            if (isset($codetail['Codetail']['Cat']) && !empty($codetail['Codetail']['Cat'])) {

                $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                $codetails1[$i]['Codetail']['Status'] = $status;
                $codetails1[$i]['Codetail']['Exch'] = $Exch;
                $i++;
            } elseif (empty($codetail['Codetail']['Cat']) && empty($codetail['Codetail']['CodeY'])) {
                //echo"sahbaj";
                $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                $codetails1[$i]['Codetail']['Status'] = $status;
                $codetails1[$i]['Codetail']['Exch'] = $Exch;
                $i++;
            } else {
                //echo $row['Cat'].$row['CodeY'].$row['ComID']."<br />";
                if ($this->Codetail->CompanynameChanges($codetail['Codetail']['ComID'], $codetail['Codetail']['CodeY'])) {
                    $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                    $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                    $codetails1[$i]['Codetail']['Status'] = $status;
                    $codetails1[$i]['Codetail']['Exch'] = $Exch;
                    $i++;
                }
            }
        }
        //pr($codetails1);
        $this->layout = false;
        $this->autoLayout = false;
        $this->set('csvfile', $id);
        $this->set('codetails', $codetails1);
        $this->render('download_csv_list');
    }

    function in_administration($id=null) {
        //echo $id;exit;
        $this->Codetail->recursive = -1;
        $codetails = $this->Codetail->find(
                'all', array("conditions" => array("Codetail.Exch <>" => "z", "Codetail.Type" => "a"),
            "order" => array("Codetail.ComName ASC")
                )
        );

        $i = 0;
        foreach ($codetails as $codetail) {
            switch ($codetail['Codetail']['Cat']) {
                case 'l': $status = 'Listed';
                    break;
                case 's': $status = 'Suspended';
                    break;
                case 'S': $status = 'Suspended';
                    break;
                case 'd': $status = 'Delisted';
                    break;
                default: $status = "Delisted";
            }
            if (empty($codetail['Codetail']['Cat']) && isset($codetail['Codetail']['CodeY']))
                $status = "Former Name";
            if (empty($codetail['Codetail']['Cat']) && empty($codetail['Codetail']['CodeY']))
                $status = "";

            switch ($codetail['Codetail']['Exch']) {
                case 'A': $Exch = 'ASX';
                    break;
                case 'N': $Exch = 'NSX';
                    break;
                case 'B': $Exch = 'BSX';
                    break;
                case 'Z': $Exch = 'NZX';
                    break;
                case 'OZ': $Exch = 'Other';
                    break;
                case 'OA': $Exch = 'Other';
                    break;
                default: $Exch = "ASX";
            }

            if (isset($codetail['Codetail']['Cat']) && !empty($codetail['Codetail']['Cat'])) {
                $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                $codetails1[$i]['Codetail']['Status'] = $status;
                $codetails1[$i]['Codetail']['Exch'] = $Exch;
                $i++;
            } elseif (empty($codetail['Codetail']['Cat']) && empty($codetail['Codetail']['CodeY'])) {
                //echo"sahbaj";
                $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                $codetails1[$i]['Codetail']['Status'] = $status;
                $codetails1[$i]['Codetail']['Exch'] = $Exch;
                $i++;
            } else {
                //echo $row['Cat'].$row['CodeY'].$row['ComID']."<br />";
                if ($this->Codetail->CompanynameChanges($codetail['Codetail']['ComID'], $codetail['Codetail']['CodeY'])) {
                    $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                    $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                    $codetails1[$i]['Codetail']['Status'] = $status;
                    $codetails1[$i]['Codetail']['Exch'] = $Exch;
                    $i++;
                }
            }
        }
        //pr($codetails1);
        $this->layout = false;
        $this->autoLayout = false;
        $this->set('csvfile', $id);
        $this->set('codetails', $codetails1);
        $this->render('download_csv_list');
    }

    function in_liquidation($id=null) {
        //echo $id;exit;
        $this->Codetail->recursive = -1;
        $codetails = $this->Codetail->find(
                'all', array("conditions" => array("Codetail.Exch <>" => "z", "Codetail.Type" => "l"),
            "order" => array("Codetail.ComName ASC"))
        );

        $i = 0;
        foreach ($codetails as $codetail) {
            switch ($codetail['Codetail']['Cat']) {
                case 'l': $status = 'Listed';
                    break;
                case 's': $status = 'Suspended';
                    break;
                case 'S': $status = 'Suspended';
                    break;
                case 'd': $status = 'Delisted';
            }
            if (empty($codetail['Codetail']['Cat']) && isset($codetail['Codetail']['CodeY']))
                $status = "Former Name";
            if (empty($codetail['Codetail']['Cat']) && empty($codetail['Codetail']['CodeY']))
                $status = "";

            switch ($codetail['Codetail']['Exch']) {
                case 'A': $Exch = 'ASX';
                    break;
                case 'N': $Exch = 'NSX';
                    break;
                case 'B': $Exch = 'BSX';
                    break;
                case 'Z': $Exch = 'NZX';
                    break;
                case 'OZ': $Exch = 'Other';
                    break;
                case 'OA': $Exch = 'Other';
                    break;
                default: $Exch = "ASX";
            }


            if (isset($codetail['Codetail']['Cat']) && !empty($codetail['Codetail']['Cat'])) {
                $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                $codetails1[$i]['Codetail']['Status'] = $status;
                $codetails1[$i]['Codetail']['Exch'] = $Exch;
                $i++;
            } elseif (empty($codetail['Codetail']['Cat']) && empty($codetail['Codetail']['CodeY'])) {
                //echo"sahbaj";
                $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                $codetails1[$i]['Codetail']['Status'] = $status;
                $codetails1[$i]['Codetail']['Exch'] = $Exch;
                $i++;
            } else {
                //echo $row['Cat'].$row['CodeY'].$row['ComID']."<br />";
                if ($this->Codetail->CompanynameChanges($codetail['Codetail']['ComID'], $codetail['Codetail']['CodeY'])) {

                    $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                    $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                    $codetails1[$i]['Codetail']['Status'] = $status;
                    $codetails1[$i]['Codetail']['Exch'] = $Exch;
                    $i++;
                }
            }
        }
        //pr($codetails1);exit;
        $this->layout = false;
        $this->autoLayout = false;
        $this->set('csvfile', $id);
        $this->set('codetails', $codetails1);
        $this->render('download_csv_list');
    }

    function deregistered($id=null) {
        //echo $id;exit;
        $this->Codetail->recursive = -1;
        $codetails = $this->Codetail->find(
                'all', array("conditions" => array("Codetail.Exch <>" => "z", "Codetail.Type" => "d"),
            "order" => array("Codetail.ComName ASC"))
        );

        $i = 0;
        foreach ($codetails as $codetail) {
            switch ($codetail['Codetail']['Cat']) {
                case 'l': $status = 'Listed';
                    break;
                case 's': $status = 'Suspended';
                    break;
                case 'd': $status = 'Delisted';
                    break;
                default: $status = "Delisted";
            }
            if (empty($codetail['Codetail']['Cat']) && isset($codetail['Codetail']['CodeY']))
                $status = "Former Name";
            if (empty($codetail['Codetail']['Cat']) && empty($codetail['Codetail']['CodeY']))
                $status = "";

            switch ($codetail['Codetail']['Exch']) {
                case 'A': $Exch = 'ASX';
                    break;
                case 'N': $Exch = 'NSX';
                    break;
                case 'B': $Exch = 'BSX';
                    break;
                case 'Z': $Exch = 'NZX';
                    break;
                case 'OZ': $Exch = 'Other';
                    break;
                case 'OA': $Exch = 'Other';
                    break;
                default: $Exch = "ASX";
            }


            if (isset($codetail['Codetail']['Cat']) && !empty($codetail['Codetail']['Cat'])) {

                $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                $codetails1[$i]['Codetail']['Status'] = $status;
                $codetails1[$i]['Codetail']['Exch'] = $Exch;
                $i++;
            } elseif (empty($codetail['Codetail']['Cat']) && empty($codetail['Codetail']['CodeY'])) {
                //echo"sahbaj";
                $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                $codetails1[$i]['Codetail']['Status'] = $status;
                $codetails1[$i]['Codetail']['Exch'] = $Exch;
                $i++;
            } else {
                //echo $row['Cat'].$row['CodeY'].$row['ComID']."<br />";
                if ($this->Codetail->CompanynameChanges($codetail['Codetail']['ComID'], $codetail['Codetail']['CodeY'])) {

                    $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                    $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                    $codetails1[$i]['Codetail']['Status'] = $status;
                    $codetails1[$i]['Codetail']['Exch'] = $Exch;
                    $i++;
                }
            }
        }
        //pr($codetails1);
        $this->layout = false;
        $this->autoLayout = false;
        $this->set('csvfile', $id);
        $this->set('codetails', $codetails1);
        $this->render('download_csv_list');
    }

    function delisted_nz($id=null) {
        //echo $id;exit;
        $this->Codetail->recursive = -1;
        $codetails = $this->Codetail->find(
                'all', array("conditions" => array("Codetail.Exch" => "z", "Codetail.Cat" => "d"))
        );

        $i = 0;
        foreach ($codetails as $codetail) {
            switch ($codetail['Codetail']['Cat']) {
                case 'l': $status = 'Listed';
                    break;
                case 's': $status = 'Suspended';
                    break;
                case 'd': $status = 'Delisted';
                    break;
                default: $status = "Delisted";
            }
            if (empty($codetail['Codetail']['Cat']) && isset($codetail['Codetail']['CodeY']))
                $status = "Former Name";
            if (empty($codetail['Codetail']['Cat']) && empty($codetail['Codetail']['CodeY']))
                $status = "";

            switch ($codetail['Codetail']['Exch']) {
                case 'A': $Exch = 'ASX';
                    break;
                case 'N': $Exch = 'NSX';
                    break;
                case 'B': $Exch = 'BSX';
                    break;
                case 'Z': $Exch = 'NZX';
                    break;
                case 'OZ': $Exch = 'Other';
                    break;
                case 'OA': $Exch = 'Other';
                    break;
                default: $Exch = "ASX";
            }


            if (isset($codetail['Codetail']['Cat']) && !empty($codetail['Codetail']['Cat'])) {

                $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                $codetails1[$i]['Codetail']['Status'] = $status;
                $codetails1[$i]['Codetail']['Exch'] = $Exch;
                $i++;
            } elseif (empty($codetail['Codetail']['Cat']) && empty($codetail['Codetail']['CodeY'])) {
                //echo"sahbaj";
                $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                $codetails1[$i]['Codetail']['Status'] = $status;
                $codetails1[$i]['Codetail']['Exch'] = $Exch;
                $i++;
            } else {
                //echo $row['Cat'].$row['CodeY'].$row['ComID']."<br />";
                if ($this->Codetail->CompanynameChanges($codetail['Codetail']['ComID'], $codetail['Codetail']['CodeY'])) {

                    $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                    $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                    $codetails1[$i]['Codetail']['Status'] = $status;
                    $codetails1[$i]['Codetail']['Exch'] = $Exch;
                    $i++;
                }
            }
        }
        //pr($codetails1);
        $this->layout = false;
        $this->autoLayout = false;
        $this->set('csvfile', $id);
        $this->set('codetails', $codetails1);
        $this->render('download_csv_list');
    }

    function listed_au($id=null) {
        //echo $id;exit;
        $this->Codetail->recursive = -1;
        $codetails = $this->Codetail->find(
                'all', array("conditions" => array("Codetail.Exch <>" => "z",
                "OR" => array(array("Codetail.Cat" => "l"), array("Codetail.Cat" => "s")))
                )
        );

        $i = 0;
        //print_r($codetails);
        foreach ($codetails as $codetail) {
            switch ($codetail['Codetail']['Cat']) {
                case 'l': $status = 'Listed';
                    break;
                case 's': $status = 'Suspended';
                    break;
                case 'd': $status = 'Delisted';
                    break;
                default: $status = "Delisted";
            }
            if (empty($codetail['Codetail']['Cat']) && isset($codetail['Codetail']['CodeY']))
                $status = "Former Name";
            if (empty($codetail['Codetail']['Cat']) && empty($codetail['Codetail']['CodeY']))
                $status = "";

            switch ($codetail['Codetail']['Exch']) {
                case 'A': $Exch = 'ASX';
                    break;
                case 'N': $Exch = 'NSX';
                    break;
                case 'B': $Exch = 'BSX';
                    break;
                case 'Z': $Exch = 'NZX';
                    break;
                case 'OZ': $Exch = 'Other';
                    break;
                case 'OA': $Exch = 'Other';
                    break;
                default: $Exch = "ASX";
            }


            if (isset($codetail['Codetail']['Cat']) && !empty($codetail['Codetail']['Cat'])) {

                $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                $codetails1[$i]['Codetail']['Status'] = $status;
                $codetails1[$i]['Codetail']['Exch'] = $Exch;
                $i++;
            } elseif (empty($codetail['Codetail']['Cat']) && empty($codetail['Codetail']['CodeY'])) {
                //echo"sahbaj";
                $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                $codetails1[$i]['Codetail']['Status'] = $status;
                $codetails1[$i]['Codetail']['Exch'] = $Exch;
                $i++;
            } else {
                //echo $row['Cat'].$row['CodeY'].$row['ComID']."<br />";
                if ($this->Codetail->CompanynameChanges($codetail['Codetail']['ComID'], $codetail['Codetail']['CodeY'])) {

                    $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                    $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                    $codetails1[$i]['Codetail']['Status'] = $status;
                    $codetails1[$i]['Codetail']['Exch'] = $Exch;
                    $i++;
                }
            }
        }
        //print_r($codetails1);
        $this->layout = false;
        $this->autoLayout = false;
        $this->set('csvfile', $id);
        $this->set('codetails', $codetails1);
        $this->render('download_csv_list');
    }

    function listed_nz($id=null) {
        //echo $id;exit;
        $this->Codetail->recursive = -1;
        $codetails = $this->Codetail->find(
                'all', array("conditions" => array("Codetail.Exch " => "z", "Codetail.Cat" => "l"))
        );

        $i = 0;
        foreach ($codetails as $codetail) {
            switch ($codetail['Codetail']['Cat']) {
                case 'l': $status = 'Listed';
                    break;
                case 's': $status = 'Suspended';
                    break;
                case 'd': $status = 'Delisted';
                    break;
                default: $status = "Delisted";
            }
            if (empty($codetail['Codetail']['Cat']) && isset($codetail['Codetail']['CodeY']))
                $status = "Former Name";
            if (empty($codetail['Codetail']['Cat']) && empty($codetail['Codetail']['CodeY']))
                $status = "";

            switch ($codetail['Codetail']['Exch']) {
                case 'A': $Exch = 'ASX';
                    break;
                case 'N': $Exch = 'NSX';
                    break;
                case 'B': $Exch = 'BSX';
                    break;
                case 'Z': $Exch = 'NZX';
                    break;
                case 'OZ': $Exch = 'Other';
                    break;
                case 'OA': $Exch = 'Other';
                    break;
                default: $Exch = "ASX";
            }


            if (isset($codetail['Codetail']['Cat']) && !empty($codetail['Codetail']['Cat'])) {

                $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                $codetails1[$i]['Codetail']['Status'] = $status;
                $codetails1[$i]['Codetail']['Exch'] = $Exch;
                $i++;
            } elseif (empty($codetail['Codetail']['Cat']) && empty($codetail['Codetail']['CodeY'])) {
                //echo"sahbaj";
                $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                $codetails1[$i]['Codetail']['Status'] = $status;
                $codetails1[$i]['Codetail']['Exch'] = $Exch;
                $i++;
            } else {
                //echo $row['Cat'].$row['CodeY'].$row['ComID']."<br />";
                if ($this->Codetail->CompanynameChanges($codetail['Codetail']['ComID'], $codetail['Codetail']['CodeY'])) {

                    $codetails1[$i]['Codetail']['Company'] = $codetail['Codetail']['ComName'];
                    $codetails1[$i]['Codetail']['Code'] = $codetail['Codetail']['Code'];
                    $codetails1[$i]['Codetail']['Status'] = $status;
                    $codetails1[$i]['Codetail']['Exch'] = $Exch;
                    $i++;
                }
            }
        }
        //pr($codetails1);
        $this->layout = false;
        $this->autoLayout = false;
        $this->set('csvfile', $id);
        $this->set('codetails', $codetails1);
        $this->render('download_csv_list');
    }

    function convert_url($title=null) {
        $patterns = array('/<(.*?)>/', '/\s+/', '/\//', '/[^a-z0-9\-\._]/', '/\+/', '/-$/', '/_$/', '/^_/', '/^-/', '/\.+$/');
        $replace = array('', '-', '-', '', '-', '', '', '', '', '');
        $string = str_replace(' - ', '-', strtolower($title));
        return preg_replace($patterns, $replace, $string);
        $this->layout = false;
        $this->autoRender = false;
    }

    function admin_url_title() {
        $this->Codetail->recursive = -1;
        $codetails = $this->Codetail->find('all', array(
            'conditions' => array('url_title' => NULL),
            'fields' => array('ComID', 'ComName', 'url_title'),
            'order' => array('ComID ASC'),
            'limit' => '0,2000'));
        $i = 0;
        //print_r($codetails);exit;
        foreach ($codetails as $codetail) {
            $this->data['Codetail']['ComID'] = $codetail['Codetail']['ComID'];
            $this->data['Codetail']['url_title'] = $this->convert_url($codetail['Codetail']['ComName']);
            if ($this->Codetail->save($this->data)) {
                $i++;
            }
        }
        echo "<br>Total row updated : " . $i;
        $this->layout = false;
        $this->autoRender = false;
    }

}

